var app = getApp()
Page({
  data: {
    peoplePrice:0,
    goodsList:[],
    laborCostTime:'0',
    laborItem:{
      id:0,
      price:0
    },
    modalPrice:'',
    modalIndex:'',
    isModal:false,
    settlementPrice:'',
    routeControl:true,
    accountId:'',
    allChoose:true,
    isEdit:false,
    isOwner:false,
    noData:false
  },
  onLoad: function (options) {
    app.getMemInfo().then(res=>{
      this.setData({
        accountId:res.data.id
      })
      this.getSettlement()
      if (res.data.role=='SHOP_OWNER') {
        this.setData({
          isOwner:true
        })
      }
    })
    if (options.type==0) {
      this.setData({
        addPeopleShow:true
      })
    }else{
      this.setData({
        addPeopleShow:true,
        routeControl:false
      })
    }
    
  },
  onShow:function(){
    this.totalPrice()
  },
  add(e){
    let count = e.currentTarget.dataset.count
    let i = e.currentTarget.dataset.index
    console.log(count,i)
    count++
    this.setData({
      ['goodsList[' + i + '].count']:count
    })
    this.totalPrice()
  },
  sub(e){
    let count = e.currentTarget.dataset.count
    let i = e.currentTarget.dataset.index
    count--
    if (count>0) {
      this.setData({
        ['goodsList[' + i + '].count']:count
      })
    }
    this.totalPrice()
  },
  totalPrice(){
    let total = 0
    for (var i = 0; i < this.data.goodsList.length; i++) {
      let count = this.data.goodsList[i].count * this.data.goodsList[i].price
      total += count
    }
    this.setData({
      settlementPrice:total+this.data.peoplePrice
    })
  },
  bindToConfirmOrder(){
     wx.navigateTo({
      url:'confirmOrder'
    })
  },
  bindToOrderDetail(){
    //创建订单
    //判断是否全选 false是全选
    let carts = []
    if (this.data.allChoose==false) {
      carts = JSON.stringify(this.data.goodsList)
    }else{
      this.data.goodsList.forEach(item=>{
        if (item.isChecked==1) {
          carts.push(item)
        }
      })
    }
    var data = {
      carts,
      accountId:this.data.accountId,
      //人工费
      // laborCostId:this.data.laborItem.id,
      // laborPrice:this.data.laborItem.price,
      // hours:this.data.laborCostTime,
      laborCostId:0,
      laborPrice:0,
      hours:0
    }
    console.log(data)
    if (carts=='') {
      app.errorMsg('请选择需要结算的商品')
    }else{      
      app.request('wx_app/user_order/add',data,'GET').then(res=>{
        console.log(res)
        let orderId = res.data.id
        wx.navigateTo({
          url:'orderDetail?orderId=' + orderId
        })
      })
    }
   
  },
  addLabor(){
    wx.navigateTo({
      url:'laborCost'
    })
  },
  bindSettlement(){

  },
  bindEditPrice(e){
    this.setData({
      modalPrice:e.currentTarget.dataset.price,
      modalIndex:e.currentTarget.dataset.index,
      isModal:true
    })
  },
  cancelModal(){
    this.setData({
      isModal:false
    })
  },
  confirmModal(){
    this.setData({
      isModal:false
    })
  },
  changePrice(e){
    console.log(e.detail.value)
    let price = parseInt(e.detail.value)
    let i = this.data.modalIndex
    this.setData({
      ['goodsList['+i+'].price']:price
    })
  },
  chooseItem(e){
    console.log(e.currentTarget.dataset.i)
    let i = e.currentTarget.dataset.i
    if (this.data.goodsList[i].isChecked==0) {
      this.setData({
        ['goodsList['+i+'].isChecked']:1
      })
    }else{
      this.setData({
        ['goodsList['+i+'].isChecked']:0
      })
    }
    let goodsList = this.data.goodsList
    let checkBox = []
    let num = 0
    goodsList.forEach(item=>{
      checkBox.push(item.isChecked)
    })
    checkBox.forEach(item=>{
      num += item
    })
    if (num==checkBox.length) {
      this.setData({
        allChoose:false
      })
    }else{
      this.setData({
        allChoose:true
      })
    }
  },
  allChoose(){
    let goodsList = this.data.goodsList
    if (this.data.allChoose==true) {
      goodsList.forEach(item=>{
        item.isChecked = 1
      })
      this.setData({
        allChoose:false,
        goodsList
      })
    }else{
      goodsList.forEach(item=>{
        item.isChecked = 0
      })
      this.setData({
        allChoose:true,
        goodsList
      })
    }
  },
  //获取购物车详情
  getSettlement(){
    let data = {
      accountId:this.data.accountId
    }
    var that = this
    app.request('wx_app/cart/product/list',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
          that.setData({
            goodsList:res.data.carts
          })
          that.totalPrice()
      }
    })
  },
  bindEdit(){
    this.setData({
      isEdit:true
    })
  },
  bindDelete(){
    var that = this
    let delBox = []
    let goodsList = this.data.goodsList
    goodsList.forEach(item=>{
      if (item.isChecked==1) {
        delBox.push(item.id)
      }
    })
    let cartId = delBox.join(',')
    console.log(cartId)
    let data = {
      cartId
    }
    app.request('wx_app/cart/product/del',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        that.getSettlement()
      }
    })
    if (this.data.goodsList.length==0) {
      this.setData({
        noData:true
      })
    }else{
      this.setData({
        noData:false
      })
    }
  },
  bindComplete(){
    this.setData({
      isEdit:false
    })

  }
})